package com.davinci.freemarket.businessLayer.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.davinci.freemarket.businessLayer.controllers.PublicationsController;
import com.davinci.freemarket.dataModelLayer.model.DBAttibuteModel;
import com.davinci.freemarket.dataModelLayer.model.PublicationModel;

@WebServlet("/UserPublicationsServlet")
public class UserPublicationsServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {

		String userID = request.getParameter("user_id");
		PrintWriter pw;
		try {
			pw = response.getWriter();

			//-- genero los parametros 	<NOMBRE_COLUMNA>, <VALOR>
			ArrayList<DBAttibuteModel> conditions = new ArrayList<DBAttibuteModel>();
			conditions.add(new DBAttibuteModel("ID_USER", userID));
			
			PublicationsController publicationsController = new PublicationsController();

			if (userID.length() < 1 || userID == null)
				return;
			
			//-- pido la puclicacion con el ID provisto
			ArrayList<PublicationModel> publications = publicationsController.getPublication(conditions);
			
			if (publications.size() > 0) {
				HttpSession session = request.getSession(true);
				session.setAttribute("publications", publications);
				RequestDispatcher dispatcher = request.getRequestDispatcher("../UserPublications.jsp");
				dispatcher.forward(request, response);
			} else {
				pw.println("la lista es 0 o null");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doPost(req, resp);
	}

}
